package acm.pta9;

import java.util.*;

public class 幸福数 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int l = sc.nextInt();
        int r = sc.nextInt();
        Map<Integer,Integer> map = new LinkedHashMap<>();
        Set<Integer> dels=new HashSet<>();
        while (l<=r){
            BitSet bitSet=new BitSet(r);
            int curr=l;
            int count=0;
            while (!bitSet.get(curr)&&curr!=1){
                count++;
                bitSet.set(curr);
                curr=fun(curr);
            }
            if (curr==1){
                bitSet.set(l,false);
                bitSet.stream().forEach(dels::add);   //沿途的点全删除
                if (isPrime(l)){
                    map.put(l,count*2);
                }else {
                    map.put(l,count);
                }
            }
            l++;
        }
        dels.forEach(map::remove);
        if (map.isEmpty()) {
            System.out.println("SAD");
        }else{
            map.forEach((k,v)-> System.out.println(k+" "+v));
        }
    }
    public static int fun(int n){
        int sum=0;
        while (n!=0){
            int i = n % 10;
            sum+=i*i;
            n/=10;
        }
        return sum;
    }
    public static boolean isPrime(int n){
        //判断素数
        for (int i = 2; i <= Math.sqrt(n); i++) {
            if (n%i==0) return false;
        }
        return true;
    }
}
